package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Log;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

/**
 * 员工登录
 * @param request
 * @param employee
 * @return
 */

    @PostMapping("/login")

    public R<Employee> login(HttpServletRequest request , @RequestBody Employee employee){
        //获取密码
        String password = employee.getPassword();
        //把密码进行uuid加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        //用数据库中的用户名和我输入的用户名比较
        qw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        System.out.println(emp);
        if (emp == null){
           return R.error("用户名不存在");
        }

        //用数据库的密码和我输入的密码进行比较
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        if (emp.getStatus()!=1){
            return  R.error("该用户已经被禁用");
        }

        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }


    /**
     * 员工退出
     * @param request
     * @return
     */

    @PostMapping("/logout")
    public R<String>logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 员工添加
     * @param request
     * @return
     */
    @Log("添加")
    @PostMapping
    public R<String> save(HttpServletRequest request ,@RequestBody Employee employee){

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //获取当前用户的id
        Long empId = (Long) request.getSession().getAttribute("employee");


        employeeService.save(employee);
        return R.success("员工添加成功");
    }


    /**
     * 员工信息分页查询
     * @param page 当前查询页码
     * @param pageSize 每页展示记录数
     * @param name 员工姓名 - 可选参数
     * @return
     */

    @GetMapping("/page")
    public R<Page>page(int page,int pageSize,String name){
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //创建lamda表达式
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        // 添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);


        //执行查询
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */

    @PutMapping
    public R<String>update(HttpServletRequest request,@RequestBody Employee employee){

        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        System.out.println(id);
        if (byId!=null){
            return R.success(byId);
        }else {
            return R.error("没有查询到该员工信息");
        }
    }



}